﻿using Furion.DatabaseAccessor;
using System;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using YFurion.Locators;

namespace YFurion.Entities
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="TKey">Type of the primary key of the entity</typeparam>
    [Serializable]
    public abstract class YEntityOfTKey<TKey> : YEntityOfTKey<TKey, MasterDbContextLocator, SlaveDbContextLocator>, IYEntityOfTKey<TKey>
    {
    }

    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="TKey">Type of the primary key of the entity</typeparam>
    /// <typeparam name="TDbContextLocator1"></typeparam>
    [Serializable]
    public abstract class YEntityOfTKey<TKey, TDbContextLocator1> : YEntityBase<TKey>, IYEntityOfTKey<TKey, TDbContextLocator1>
        where TDbContextLocator1 : class, IDbContextLocator
    {
    }

    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="TKey">Type of the primary key of the entity</typeparam>
    /// <typeparam name="TDbContextLocator1"></typeparam>
    /// <typeparam name="TDbContextLocator2"></typeparam>
    [Serializable]
    public abstract class YEntityOfTKey<TKey, TDbContextLocator1, TDbContextLocator2> : YEntityBase<TKey>, IYEntityOfTKey<TKey, TDbContextLocator1, TDbContextLocator2>
        where TDbContextLocator1 : class, IDbContextLocator
        where TDbContextLocator2 : class, IDbContextLocator
    {
    }

    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="TKey">Type of the primary key of the entity</typeparam>
    /// <typeparam name="TDbContextLocator1"></typeparam>
    /// <typeparam name="TDbContextLocator2"></typeparam>
    /// <typeparam name="TDbContextLocator3"></typeparam>
    [Serializable]
    public abstract class YEntityOfTKey<TKey, TDbContextLocator1, TDbContextLocator2, TDbContextLocator3> : YEntityBase<TKey>, IYEntityOfTKey<TKey, TDbContextLocator1, TDbContextLocator2, TDbContextLocator3>
        where TDbContextLocator1 : class, IDbContextLocator
        where TDbContextLocator2 : class, IDbContextLocator
        where TDbContextLocator3 : class, IDbContextLocator
    {
    }

    public abstract class YEntityBase<TKey> : IYEntityBase<TKey>
    {
        /// <summary>
        /// Unique identifier for this entity.
        /// </summary>
        [Key]
        public virtual TKey Id { get; set; }

        /// <inheritdoc/>
        public override bool Equals(object obj)
        {
            if (obj == null || !(obj is YEntityBase<TKey>))
            {
                return false;
            }

            //Same instances must be considered as equal
            if (ReferenceEquals(this, obj))
            {
                return true;
            }

            //Transient objects are not considered as equal
            YEntityBase<TKey> other = (YEntityBase<TKey>)obj;

            //Must have a IS-A relation of types or must be same type
            Type typeOfThis = GetType();
            Type typeOfOther = other.GetType();
            if (!typeOfThis.GetTypeInfo().IsAssignableFrom(typeOfOther) && !typeOfOther.GetTypeInfo().IsAssignableFrom(typeOfThis))
            {
                return false;
            }

            return Id.Equals(other.Id);
        }

        /// <inheritdoc/>
        public override int GetHashCode()
        {
            if (Id == null)
            {
                return 0;
            }

            return Id.GetHashCode();
        }

        /// <inheritdoc/>
        public static bool operator ==(YEntityBase<TKey> left, YEntityBase<TKey> right)
        {
            if (Equals(left, null))
            {
                return Equals(right, null);
            }

            return left.Equals(right);
        }

        /// <inheritdoc/>
        public static bool operator !=(YEntityBase<TKey> left, YEntityBase<TKey> right)
        {
            return !(left == right);
        }

        /// <inheritdoc/>
        public override string ToString()
        {
            return $"[{GetType().Name}#{Id}]";
        }
    }
}
